package com.meng.mmlive;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.lqr.emoji.LQREmotionKit;
import com.meng.hotfix.HotFix;
import com.meng.mmlive.dragger2.component.AppComponent;
import com.meng.mmlive.dragger2.component.DaggerAppComponent;
import com.meng.mmlive.dragger2.component.HomeComponent;
import com.meng.mmlive.dragger2.component.SessionComponent;
import com.meng.mmlive.dragger2.module.AppModule;
import com.meng.mmlive.dragger2.module.HomeModule;
import com.meng.mmlive.dragger2.module.SessionModule;

import java.lang.reflect.Method;

import de.greenrobot.event.EventBus;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;
import io.rong.message.TextMessage;

/**
 * Created by Administrator on 2017/5/13.
 */

public class App extends Application implements RongIMClient.OnReceiveMessageListener {
    private AppComponent appComponent;
    private HomeComponent homeComponent;
    private SessionComponent sessionComponent;
    public static App instance;
    public int screenW;
    public int screenH;
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        Fresco.initialize(this);
        initRongCloud();
        LQREmotionKit.init(this);
        createAppComponent();
        HotFix.getInstance().init(this);
        HotFix.getInstance().injectAssets("jiemi.jar");


    }

    private void initRongCloud() {
        /**
         * OnCreate 会被多个进程重入，这段保护代码，确保只有您需要使用 RongIMClient 的进程和 Push 进程执行了 init。
         * io.rong.push 为融云 push 进程名称，不可修改。
         */
        if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext())) ||
                "io.rong.push".equals(getCurProcessName(getApplicationContext()))) {
            RongIMClient.init(this);
        }
        //监听接收到的消息
        RongIMClient.setOnReceiveMessageListener(this);
    }

    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
    private void createAppComponent() {
//        appComponent = DaggerAppComponent.builder()
//                .appMondule(new AppModule(this)).build();
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this)).build();
    }
    public HomeComponent getHomeComponent(){
        if(homeComponent == null){
            homeComponent = appComponent.plus(new HomeModule());
        }
        return homeComponent;
    }
    public SessionComponent getSessionComponent(){
        if(sessionComponent == null){
            sessionComponent = appComponent.plus(new SessionModule());
        }
        return sessionComponent;
    }
    private static Method method;
    public static String getStr(String str,String str2){
        if(method == null){
            try {
                Class<?> aClass = Class.forName("company.chat.coquettish.android.m.e");
                method = aClass.getDeclaredMethod("a", String.class, String.class);
            } catch (Exception e) {
            }
        }
        Object invoke = null;
        try {
            invoke = method.invoke(null, str, str2);
            return invoke.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean onReceived(Message message, int i) {
        Log.d("App", "app received msg");
        MessageContent messageContent = message.getContent();
        EventBus.getDefault().post(message);
        if(messageContent instanceof TextMessage){
            EventBus.getDefault().post((TextMessage)messageContent);
        }
        return false;
    }
}
